package com.csun.cmny.provider.controller;

import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.dto.MonthlyCountQueryDto;
import com.csun.cmny.provider.model.vo.*;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OldmanService;
import com.csun.cmny.provider.service.OmcIndexHomeService;
import com.csun.cmny.provider.service.StatisticsService;
import com.csun.cmny.provider.utils.DateUtil;
import com.csun.cmny.provider.vo.*;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.collections.CollectionUtils;
import org.joda.time.LocalDate;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author chenzheng
 * @description:统计分析模块控制层
 * @date 2019/9/4 11:03
 */
@RestController
@RequestMapping(value = "/statistics", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class OmcStatisticsController extends BaseController {
    @Resource
    private CmnyMessageSource cmnyMessageSource;

    @Resource
    private StatisticsService statisticsService;

    @Resource
    private OmcIndexHomeService omcIndexHomeService;

    @Resource
    private OldmanService oldmanService;

    /**
     * 一月份
     */
    private static final Integer JANUARY = 1;


    /**
     * 通过楼栋ID获取楼栋量化统计老年服务部统计数据
     *
     * @param buildingId
     * @return
     */
    @RequestMapping(value = "/getStatisticsData", method = RequestMethod.POST)
    public Wrapper getStatisticsData(@RequestParam("buildingId") Integer buildingId) {
        OldServerVo oldServerVo = new OldServerVo();
        //机构ID
        Long groupId = getLoginAuthDto().getGroupId();
        //当前时间
        LocalDate now = LocalDate.now();
        //当前年份
        Integer year = now.getYear();
        //当前月份
        int month = now.getMonthOfYear();
        //上一月
        Integer lastMonth = 0;
        //上一年
        Integer lastYear = 0;
        if (month == JANUARY) {
            lastMonth = 12;
            lastYear = year - 1;
        } else {
            lastYear = year;
            lastMonth = month - 1;
        }
        //本月入院人数
        oldServerVo.setPresentMonthInCount(statisticsService.getOldmanCountByYearAndMonth(year, month, groupId));
        //上月入院人数
        oldServerVo.setLastMonthInCount(statisticsService.getOldmanCountByYearAndMonth(lastYear, lastMonth, groupId));
        //本月院内总人数
        oldServerVo.setPresentMonthAllInCount(statisticsService.getAllOldmanCountByYearAndMonth(year, month, groupId));
        //上月院内总人数
        oldServerVo.setLastMonthAllInCount(statisticsService.getAllOldmanCountByYearAndMonth(lastYear, lastMonth, groupId));
        //当前月份去世老人总数
        oldServerVo.setDeadOldmanCount(statisticsService.getDeadOldmanCountByYearAndCount(year, month, groupId));
        List<Integer> floorNos = statisticsService.getAllFloorNoByBuildingId(buildingId);
        List<FloorDataVo> floorDatas = new ArrayList<>();
        FloorDataVo floorData = null;
        for (Integer floorNo : floorNos) {
            floorData = new FloorDataVo();
            floorData.setFloorNo(floorNo);
            //当月被使用的床位数
            floorData.setLastMonthBedUsedCount(statisticsService.getBedCountByBuildingIdAndFloorNo(year, month, buildingId, floorNo));
            //上月被使用的床位数
            floorData.setLastMonthBedUsedCount(statisticsService.getBedCountByBuildingIdAndFloorNo(lastYear, lastMonth, buildingId, floorNo));
            floorDatas.add(floorData);
        }

        oldServerVo.setFloorDatas(floorDatas);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("comment.query.success", null), oldServerVo);
    }

    /**
     * 每月入住老人数量统计
     *
     * @return
     */
    @RequestMapping(value = "/oldMangrow", method = RequestMethod.POST)
    public Wrapper oldMangrow() {
        Long groupId = getLoginAuthDto().getGroupId();
        String date = DateUtil.getDate_yearMonth();
        //取得日期月份
        Integer month = Integer.parseInt(date.split("-")[1]);
        //返回结果集合
        List<OldMonthDataVo> list = new ArrayList<>();
        //取得日期年份
        String year = date.split("-")[0];
        for (int i = 1; i <= month; i++) {
            String day = "";
            if (i < 10) {
                day = "0" + i;
            } else {
                day = String.valueOf(i);
            }
            date = year + "-" + day;
            Integer n = statisticsService.selectByDate(groupId, date);
            OldMonthDataVo dto = new OldMonthDataVo();
            dto.setMonth(i);
            dto.setInCount(n);
            list.add(dto);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("comment.query.success", null), list);
    }


    /**
     * 今日生日和今日请假统计
     *
     * @return
     */
    @RequestMapping(value = "/blStatistics", method = RequestMethod.POST)
    public Wrapper blStatistics() {
        Long groupId = getLoginAuthDto().getGroupId();
        OldBirthdayVo oldBirthdayVo = new OldBirthdayVo();
        //今日预约（咨询）、今日试住、今日外出（请假）
        //获取当日日期
        String thisDate = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        List<Integer> todayList = omcIndexHomeService.todayList(groupId, thisDate);
        oldBirthdayVo.setTodayOut(todayList.get(2));//今日外出（请假）
        //查询某天生日的老人集合
        String today = DateUtil.getDate_dtShort();
        String date = today.substring(5);

        //今日生日老人集合
        Map<String, Object> queryParam = new HashMap<>();
        queryParam.put("groupId", groupId);
        queryParam.put("date", date);
        List<OldManVo> list = oldmanService.queryOldManBirthdayByDay(queryParam);
        int birNumber = 0;
        if (!CollectionUtils.isEmpty(list)) {
            birNumber = list.size();
        }
        oldBirthdayVo.setBirNumber(birNumber);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("comment.query.success", null), oldBirthdayVo);
    }

    /**
     * 老人年龄统计
     *
     * @return
     */
    @RequestMapping(value = "/oldAgeStatistics", method = RequestMethod.POST)
    public Wrapper oldAgeStatistics() {
        Long groupId = getLoginAuthDto().getGroupId();
        List<Integer> list = statisticsService.oldAgeStatistics(groupId);
        OldAgeDataVo oldAgeDataVo = new OldAgeDataVo();
        Map<String, Object> map = new HashMap<>();
        //总数
        oldAgeDataVo.setTotal(list.get(0));
        //年龄60以下
        oldAgeDataVo.setLessSixty(list.get(1));
        //年龄60-69
        oldAgeDataVo.setSixty(list.get(2));
        //年龄70-79
        oldAgeDataVo.setSeventy(list.get(3));
        //年龄80-89
        oldAgeDataVo.setEighty(list.get(4));
        //年龄90-99
        oldAgeDataVo.setNinety(list.get(5));
        //年龄100及以上
        oldAgeDataVo.setHundred(list.get(6));
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("comment.query.success", null), oldAgeDataVo);
    }

    /**
     * 机构老人人员类别人数统计
     *
     * @return
     */
    @RequestMapping(value = "/oldTypeStatistics", method = RequestMethod.POST)
    public Wrapper oldTypeStatistics() {
        Long groupId = getLoginAuthDto().getGroupId();
        List<OldTypeDataVo> list = statisticsService.oldTypeStatistics(groupId);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("comment.query.success", null), list);
    }

    /**
     * 机构老人类别人数统计 -- 自理状态
     *
     * @return
     */
    @RequestMapping(value = "/oldManTypeStatistics", method = RequestMethod.POST)
    public Wrapper oldManTypeStatistics() {
        Long groupId = getLoginAuthDto().getGroupId();
        //OldSelfDataVo oldSelfDataVo = statisticsService.oldManTypeStatistics(groupId);
        List<OldSelfDataNewVo> vos=statisticsService.newOldManTypeStatistics(groupId);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("comment.query.success", null), vos);
    }

    /**
     * 老人性别统计
     *
     * @return
     */
    @RequestMapping(value = "/sexStatistics", method = RequestMethod.POST)
    public Wrapper sexStatistics() {
        Long groupId = getLoginAuthDto().getGroupId();
        OldSexDataVo oldSexDataVo = statisticsService.oldManSexStatistics(groupId);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("comment.query.success", null), oldSexDataVo);
    }

    /**
     * 老人入院月度汇总
     *
     * @return
     */
    @RequestMapping(value = "/oldManInDate", method = RequestMethod.POST)
    public Wrapper oldManInDate(@RequestBody MonthlyCountQueryDto monthlyCountQueryDto) {
        monthlyCountQueryDto.setGroupId(getLoginAuthDto().getGroupId());
        PageInfo pageInfo = statisticsService.selectByInOldMan(monthlyCountQueryDto);
        //return WrapMapper.ok(pageInfo);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),pageInfo);

    }

    /**
     * 老人出院月度汇总
     *
     * @return
     */
    @RequestMapping(value = "/oldManOutDate",method = RequestMethod.POST)
    public Wrapper oldManOutDate(@RequestBody MonthlyCountQueryDto monthlyCountQueryDto){
       monthlyCountQueryDto.setGroupId(getLoginAuthDto().getGroupId());
        PageInfo pageInfo = statisticsService.selectByOutOldMan(monthlyCountQueryDto);
        //return WrapMapper.ok(pageInfo);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),pageInfo);
    }

    /**
     * 调房调级月度汇总
     *
     * @return
     */
    @RequestMapping(value = "/oldManUpdateRoom",method = RequestMethod.POST)
    public Wrapper oldManUpdateRoom(@RequestBody MonthlyCountQueryDto monthlyCountQueryDto){
        monthlyCountQueryDto.setGroupId(getLoginAuthDto().getGroupId());
        PageInfo pageInfo = statisticsService.selectByUpdateOldMan(monthlyCountQueryDto);
        //return WrapMapper.ok(pageInfo);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),pageInfo);
    }

    /**
     * 每月老人出院,入院,调房调级数量统计
     * @param year
     * @return
     */
    @RequestMapping(value = "/oldManMonthCount",method = RequestMethod.POST)
    public Wrapper oldManMonthCount(@RequestParam("year") Integer year ){
        Long groupId = getLoginAuthDto().getGroupId();
        int[] mon = {1,2,3,4,5,6,7,8,9,10,11,12};
        Map map = null;
        Map<String,int[]> countMap = new HashMap<>();
        int[] in = new int[mon.length];
        int[] out = new int[mon.length];
        int[] update = new int[mon.length];
        for (int month:mon){
            map = new HashMap();
            map.put("year",year);
            map.put("month",month);
            map.put("groupId",groupId);
            OldManMonthCountVo vo = statisticsService.selectByMonthCount(map);
            in[month-1] = vo.getInCount();
            out[month-1] = vo.getOutCount();
            update[month-1] = vo.getUpdateCount();
        }
        countMap.put("inCount",in);
        countMap.put("outCount",out);
        countMap.put("updateCount",update);
        //return WrapMapper.ok(countMap);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),countMap);
    }

    /**
     * 按月查询老人生日
     * @param month
     * @return
     */
    @RequestMapping(value = "/oldManMonthCount/{month}",method = RequestMethod.POST)
    public Wrapper listOldmanByBirthMonth(@PathVariable String month){
        List<OldManVo> volist = statisticsService.listOldmanByBirthMonth(month,getLoginAuthDto());
        //return WrapMapper.ok(volist);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),volist);
    }
}


